
package crawler;

import db.MySQLDataManager;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 *
 * @author MT140QuyenNB
 */

public class TestThread implements Runnable{

    //UserManager usermanager;
    Thread runner;

    public TestThread()
    {
      //  usermanager = new UserManager();
        runner = new Thread(this);
        runner.start();
    }

    public void run()
    {        
        while(true)
        {                      
            try {                        
                if(checkTime(23, 59))
                {                   
                   //getResultFrom_xoso88.insertResultInDB();
                   getResultFrom_xoso88.processData();
                }
            } catch (Exception ex){}
        }

    }

    public static boolean insertResultInDB()
    {
        boolean check = false;
        StringBuffer result = new StringBuffer();
        String quyen = null;
        try {
            URL my_url = new URL("http://www.xosothudo.com.vn/default.aspx?tabid=424");

            BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));
            String strTemp = "";

            System.out.println(strTemp);

            while(null != (strTemp = br.readLine())){
                //System.out.println(strTemp);
                strTemp = ltrim(strTemp.replaceAll("( )+", " ")).replace("\"", " ");
               // System.out.println(strTemp);

                if(strTemp.startsWith("<span id= ctl04_ctl07_lb")&& strTemp.contains("style= display:inline-block;width"))
                {
                    String sub_str = strTemp.replaceAll("<span id= ctl04_ctl07_lb", "");
                    String test = sub_str.replaceAll("&nbsp", "").replaceAll(";", "").replaceAll("<br>", "").replaceAll("</span></td>","");
                    //System.out.println(sub_str);
                   // System.out.println(test.substring(test.indexOf(">")+1));
                    String toi =  test.substring(test.indexOf(">")+1);
                    quyen = result.append(toi).append(":").toString();

                }
            }
            if(quyen.length() == 115)
            {
                MySQLDataManager datamanager = new MySQLDataManager();
                //datamanager.insertResultInDB(result.toString());
                check = true;
                Debug.println("Luu duoc khong:"+ check );
                Debug.println("RESULT :" +quyen);
            }
             else{
                Debug.println("Luu duoc khong:"+ check );
                Debug.println("RESULT :" +quyen);
             }
            
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            Debug.println(ex.getMessage());
        }
        return check;
    }

    public static String removeSpaces(String s) {
      StringTokenizer st = new StringTokenizer(s," ",false);
      String t="";
      while (st.hasMoreElements()) t += st.nextElement();
      return t;
    }

    /* remove leading whitespace */
    public static String ltrim(String source) {
        return source.replaceAll("^\\s+", "");
    }

    public static String reverseIt(String source) {
        int i, len = source.length();
        StringBuilder dest = new StringBuilder(len);

        for (i = (len - 1); i >= 0; i--)
          dest.append(source.charAt(i));
        return dest.toString();
      }

    public static boolean checkTime(int hour, int minute)
    {
            boolean check = false;

            Date date = new Date();   // given date
            Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
            calendar.setTime(date);   // assigns calendar to given date
            calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
            calendar.get(calendar.MINUTE);
            calendar.get(calendar.SECOND);
            
            if ((calendar.get(Calendar.HOUR_OF_DAY) == hour)&&(calendar.get(calendar.MINUTE) == minute))
            {
                check = true;
            }
            return check;
    }

    public static int getHour()
    {
            Date date = new Date();   // given date
            Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
            calendar.setTime(date);   // assigns calendar to given date

            return calendar.get(Calendar.HOUR_OF_DAY);
    }

}
